package com.space.school.dao.impl;

import com.space.school.dao.UserDao;
import com.space.school.domain.User;
import com.space.school.utils.SpaceJDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.*;

public class UserDaoImpl implements UserDao {
//    获取数据库链接
    private JdbcTemplate jdbcTemplate=new JdbcTemplate(SpaceJDBCUtils.getDataSource());
    @Override
    public List<User> findAll(int start, int pageSize) {
//        String sql = "SELECT userId,userName,passWord,case  when status=1  THEN '生效' else '失效' END status,createTime,updateTime FROM user";
        String sql = "select * from user order by userid limit ?,?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class), start, pageSize);
    }

    @Override
    public int count(Map<String, String[]> map) {
        ArrayList<Object> params=new ArrayList<>();
        StringBuffer sql=new StringBuffer("select count(*) from user where 99=99 ");
        Set<String> set=map.keySet();
        for(String key:set){
            if(!map.get(key)[0].equals("") && !key.equals("currentPage")){
                sql.append(" and "+key+ " like ?" );
                params.add("%"+map.get(key)[0]+"%");
            }
        }
        return jdbcTemplate.queryForObject(sql.toString(), Integer.class,params.toArray());
    }

    @Override
    public void del(int userId){
        String sql = "delete from user where userId=?";
        jdbcTemplate.update(sql,userId);
    }

    @Override
    public void update(User user) {
        String sql="UPDATE user SET password = ? ,updateTime=? WHERE userId = ?";
        jdbcTemplate.update(sql,user.getPassWord(),new Date(),user.getUserId());
    }

    @Override
    public List<User> find(int userId) {
        String sql = "select * from user where userId = ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class),userId);
    }

    @Override
    public void update_status(User user) {
        String sql="UPDATE user SET status=?,updateTime=?WHERE userId = ?";
        jdbcTemplate.update(sql,user.getStatus(),new Date(),user.getUserId());
    }

    @Override
    public List<User> search(Map<String, String[]> map,int currentPage, int pageSize) {
        StringBuffer sql=new StringBuffer("select * from user where 99=99 ");
        //        取值的集合
        ArrayList<Object> params=new ArrayList<>();

//        keySet 取map中的建的数组
//        [{用户名={11}},{生效表示={生效}}]
        Set<String> set=map.keySet();
        for(String key:set){
            if(!map.get(key)[0].equals("") && !key.equals("currentPage")){
                sql.append(" and "+key+ " like ?" );
                params.add("%"+map.get(key)[0]+"%");
            }

        }
        params.add(currentPage);
        params.add(pageSize);
        sql.append(" order by userid limit ?,?");

        List<User>A=jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<User>(User.class), params.toArray());
        return A;



    }

}
